<?php
/**
 * Project:	BBCodeAdvanced
 * File:	class.uploadFile.php
 *
 * @link http://code.google.com/u/kendertas/
 * @copyright 2009 KenderTas
 * @author Kender Tas <kendertas at gmail dot com>, original class by Hasan Yasar <ejder at turk-php dot com>
 * @package classes
 * @version 1.0.0
 */

/**
*	Class to upload file, and if file is an image, it can be resize, create thumbnailes
*
*	@author		Hasan Yasar, modified by kender
*	@package	classes
*	@version	$Revision: 1.2 $
*/

class Upload
{
	var $directory_name;
	var $max_filesize;
	var $error;

	var $user_tmp_name;
	var $user_file_name;
	var $user_file_size;
	var $user_file_type;
	var $user_full_name;
	var $thumb_name;
	var $requiredGDForGIF	= 2028;	//	2.0.28 or higher

	function gdGetVersion()	{
		$gdArray	= gd_info();
		$version	= ereg_replace('[[:alpha:][:space:]()]+', '', $gdArray['GD Version']);
		$gdVersion	= Str_Replace(".","",$version);
		$supportGIF	= $this->requiredGDForGIF - $gdVersion;
		//die($this->requiredGDForGIF." - ".$gdVersion);
		if ($supportGIF <= 0)	{
			return true;
		} else {
			return false;
		}
	}

	function set_directory($dir_name = ".")
	{
		// create Dir if not exists	//
		if (!file_exists($dir_name))	{
			$patharr	= explode("/", $dir_name);
			$progr		= "";
			foreach($patharr as $k => $v)	{
				$progr		.= $v;
				if (!file_exists($progr))	{
					$oldumask	= umask(0);
					//echo $progr."<br>";
					if (mkdir($progr, 0777) == FALSE)	{
						trigger_error("impossibile creare la cartella '".$progr."' da ".$dir_name."", E_USER_ERROR);
					}
					@umask($oldumask);
					//	create and empty 'index' to prevent view of folder	//
					if (!file_exists($progr."/index.htm"))	{
						touch($progr."/index.htm");
					}
				}
				$progr	.= "/";
			}
		}
		$this->directory_name = $dir_name;
	}

	function set_max_size($max_file = 300000)	{
		$this->max_filesize = $max_file;
	}

 	function error()	{
		return $this->error;
	}

 	function is_ok()	{
		if(isset($this->error))
			return FALSE;
		else
			return TRUE;
	}

	function set_tmp_name($temp_name)	{
		$this->user_tmp_name = $temp_name;
	}

	function set_file_size($file_size)	{
		$this->user_file_size = $file_size;
	}

	function set_file_type($file_type)	{
		$this->user_file_type = $file_type;
	}

 	function set_file_name($file)	{
		$this->user_file_name = $file;
		$this->user_full_name = $this->directory_name."/".$this->user_file_name;
	}

	function resize($max_width = 0, $max_height = 0 )	{

		$infoImgForFileType	= @GetImageSize($this->user_full_name);
		$fileTypeFound	= (isset($infoImgForFileType['mime']) && $infoImgForFileType['mime'] != "")	? $infoImgForFileType['mime'] : $infoImgForFileType[2];
		if (!isset($infoImgForFileType['mime']) || $infoImgForFileType['mime'] == "")	{
			switch ($fileTypeFound)	{
				default:		$fileType	= ":666:";	break;
				case "1":		$fileType	= "gif";	break;
				case "2":		$fileType	= "jpeg";	break;
				case "3":		$fileType	= "png";	break;
			}
		} else {
			$fileType	= $fileTypeFound;
		}
		//die($fileTypeFound." == ".$fileType);
		//echo $fileTypeFound." == ".$fileType;
		/*
		1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
		7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2,
		13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM
		*/

		if (StriStr(StrToLower($fileType),"png"))	{
			$img = @ImageCreateFromPNG ($this->user_full_name);
		}

		if (StriStr(StrToLower($fileType),"jpeg") || StriStr(StrToLower($fileType),"jpg"))	{
			$img = @ImageCreateFromJPEG ($this->user_full_name);
		}

		if (StriStr(StrToLower($fileType),"gif"))	{
			if ($this->gdGetVersion() == true)	{
				$img = @ImageCreateFromGif ($this->user_full_name);
			} else {
				$this->error = "File ".$fileType." non supportato!";
			}
		}
		//echo $img;

		if (StriStr(StrToLower($fileType),":666:"))	{
			$this->error = "File non supportato!";
		}

		if (!isset($this->error))	{
			$FullImage_width	= imagesx ($img);
			$FullImage_height	= imagesy ($img);

			if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)	{
				$new_width = $max_width;
				$new_height = $max_height;
			}
			else if(isset($max_width) && $max_width != 0)	{
				$new_width = $max_width;
				$new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
			}
			else if(isset($max_height) && $max_height != 0)	{
				$new_height = $max_height;
				$new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
			} else {
				$new_height = $FullImage_height;
				$new_width = $FullImage_width;
			}
/*
    	$ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
    	$new_width = ((int)($FullImage_width * $ratio));    //full size width
    	$new_height = ((int)($FullImage_height * $ratio));    //full size height

    	$ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
    	$new_width = ((int)($new_width * $ratio));    //mid size width
    	$new_height = ((int)($new_height * $ratio));    //mid size height
*/

			$full_id	=  ImageCreateTrueColor ( $new_width , $new_height );
			ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );

			if (StriStr(StrToLower($fileType),"jpeg") || StriStr(StrToLower($fileType),"jpg"))	{
				$full = @ImageJPEG( $full_id, $this->user_full_name,100);
			}

			if (StriStr(StrToLower($fileType),"png"))	{
				$full = @ImagePNG( $full_id, $this->user_full_name);
			}

			if (StriStr(StrToLower($fileType),"gif"))	{
				if ($this->gdGetVersion() == true)	{
					$img = @ImageGIF ($full_id, $this->user_full_name);
				} else {
					$this->error = "File ".$fileType." non supportato!";
				}
			}

			ImageDestroy( $full_id );
			unset($max_width);
			unset($max_height);
		}
	}

	function start_copy()	{

		if(!isset($this->user_file_name))
			$this->error = "Specifica un file da caricare!";

		if ($this->user_file_size <= 0)
			$this->error = "Dimensione file errata (0): $this->user_file_size KB<br>";

		if ($this->user_file_size > $this->max_filesize)
			$this->error = "Dimensione file errata (1): $this->user_file_size KB<br>";

        if (!isset($this->error))	{
			$filename = basename($this->user_file_name);

		if (!empty($this->directory_name))
			$this->destination = $this->user_full_name;
		else
			$this->destination = $filename;
/* file exists control.. if file is exists it upload new file name
			if (file_exists($destination))
            {
            	srand((double)microtime()*1000000);
            	$filename = rand(0,20000)."_".$filename;
            	if (!empty ($this->directory_name) )
					$destination = $this->directory_name."/".$filename;
				else
					$destination = $filename;
            }
*/
		if(!@is_uploaded_file($this->user_tmp_name))
			$this->error = "Il file ".$this->user_tmp_name." non � stato caricato correttamente.";

		if (!@move_uploaded_file ($this->user_tmp_name,$this->destination))
			$this->error = "Impossibile copiare ".$this->user_file_name." alla cartella di destinazione.";
		}
	}

	function set_thumbnail_name($thumbname)	{
		$this->thumb_name = $this->directory_name."/".$thumbname;
		/*
		if(eregi("\.png$",$this->user_full_name))
			$this->thumb_name = $this->directory_name."/".$thumbname.".png";

		if(eregi("\.(jpg|jpeg)$",$this->user_full_name))
			$this->thumb_name = $this->directory_name."/".$thumbname.".jpg";

		if(eregi("\.gif$",$this->user_full_name))
			$this->thumb_name = $this->directory_name."/".$thumbname.".gif";
		}
		*/
	}

	function create_thumbnail()	{
		if (!@copy($this->user_full_name, $this->thumb_name))	{
			$this->error	= "<br>".$this->user_full_name.", ".$this->thumb_name."<br>";
			$this->error	.= "impossibile copiare ".$this->user_full_name."...<br />\n";
		}
	}

	function set_thumbnail_size($max_width = 0, $max_height = 0 )	{

		$infoImgForFileType	= @GetImageSize($this->user_full_name);
		$fileTypeFound	= (isset($infoImgForFileType['mime']) && $infoImgForFileType['mime'] != "")	? $infoImgForFileType['mime'] : $infoImgForFileType[2];
		if (!isset($infoImgForFileType['mime']) || $infoImgForFileType['mime'] == "")	{
			switch ($fileTypeFound)	{
				default:		$fileType	= ":666:";	break;
				case "1":		$fileType	= "gif";	break;
				case "2":		$fileType	= "jpeg";	break;
				case "3":		$fileType	= "png";	break;
			}
		} else {
			$fileType	= $fileTypeFound;
		}

		/*
		1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
		7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2,
		13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM
		*/

		if (StriStr(StrToLower($fileType),"png"))	{
			$img = @ImageCreateFromPNG ($this->thumb_name);
		}

		if (StriStr(StrToLower($fileType),"jpeg") || StriStr(StrToLower($fileType),"jpg"))	{
			$img = @ImageCreateFromJPEG ($this->thumb_name);
		}

		if (StriStr(StrToLower($fileType),"gif"))	{
			if ($this->gdGetVersion() == true)	{
				$img = @ImageCreateFromGif ($this->thumb_name);
			} else {
				$this->error = "File ".$fileType." non supportato!";
			}
		}

		if (StriStr(StrToLower($fileType),":666:"))	{
			$this->error = "File non supportato!";
		}

		$FullImage_width = imagesx ($img);
		$FullImage_height = imagesy ($img);

		if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)	{
			$new_width = $max_width;
			$new_height = $max_height;
		} else if(isset($max_width) && $max_width != 0)	{
			$new_width = $max_width;
			$new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
		} else if(isset($max_height) && $max_height != 0)	{
			$new_height = $max_height;
			$new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
		} else {
			$new_height = $FullImage_height;
			$new_width = $FullImage_width;
		}
/*
    	$ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
    	$new_width = ((int)($FullImage_width * $ratio));    //full size width
    	$new_height = ((int)($FullImage_height * $ratio));    //full size height

    	$ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
    	$new_width = ((int)($new_width * $ratio));    //mid size width
    	$new_height = ((int)($new_height * $ratio));    //mid size height
*/
		$full_id	=  ImageCreateTrueColor ( $new_width , $new_height );
		ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );

		if (StriStr(StrToLower($fileType),"jpeg") || StriStr(StrToLower($fileType),"jpg"))	{
			$full = @ImageJPEG( $full_id, $this->thumb_name,100);
		}

		if (StriStr(StrToLower($fileType),"png"))	{
			$full = @ImagePNG( $full_id, $this->thumb_name);
		}

		if (StriStr(StrToLower($fileType),"gif"))	{
			if ($this->gdGetVersion() == true)	{
				$img = @ImageGif ($full_id, $this->thumb_name);
			} else {
				$this->error = "File ".$fileType." non supportato!";
			}
		}

		ImageDestroy( $full_id );
		unset($max_width);
		unset($max_height);
	}
}

?>